package edu.krazy.engine;

/**
 * @(#)Controller.java
 *
 *
 * @author 
 * @version 1.00 2011/4/7
 */
import java.awt.event.*;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;




public class Controller implements KeyListener{

   Kart3D myKart;
   Camera myCamera;
   final float  MAX_SPEED= 1.5f;
  boolean b_accel = false;

    public Controller(Kart3D kart, Camera c) {
    	myKart = kart;
    	myCamera = c;
    }
    
    
    
    public void keyReleased (KeyEvent evt)
    {
    	 if(evt.getKeyCode() == KeyEvent.VK_A){
    	 	b_accel = false;
    	 }
    }
    
    public void keyPressed (KeyEvent evt){
    //	System.out.println("myController keyPressed");	
    
       if(evt.getKeyCode() == KeyEvent.VK_A){
        	
        			if(myKart.accel<1)
        			{
        				myKart.accel+=.0025f;
        			}
        			
        			b_accel= true;
        			//moveByLocalAxis(Utils.NORTH);
        			
        			//myCamera.moveByZ(-.1f);	
        	
        		
        }
    	
        if(evt.getKeyCode() == KeyEvent.VK_DOWN){
        	

        		
        			//moveByLocalAxis(Utils.SOUTH);
        		
        	}
        	
         if(evt.getKeyCode() == KeyEvent.VK_LEFT){
        	
					   if(myKart.accel>=0.001)
        				rotate(-1.5f);
        				
        				
   
    	    
        }
    	
        if(evt.getKeyCode() == KeyEvent.VK_RIGHT){
    		 if(myKart.accel>=0.001)
        		rotate(1.5f);
        	
        			
        
        }	
    		
    }
    public void keyTyped (KeyEvent evt)
    {
    	
    }
    
    public void update()
    {
    	
    	if(myKart.accel > 0.001){
    		float offset = MAX_SPEED*myKart.accel;
    		//move along accel vector
    		myKart.setPositionX(myKart.getPosition().getX()+myKart.accelVector.getX()*offset);
    		myKart.setPositionZ(myKart.getPosition().getZ()+myKart.accelVector.getZ()*offset);
    		
    		myKart.target.setX(myKart.target.getX()+myKart.accelVector.getX()*offset);
    		myKart.target.setZ(myKart.target.getZ()+myKart.accelVector.getZ()*offset);
    		myCamera.setPositionX(myCamera.getPositionX()+myKart.accelVector.getX()*offset);
 		    myCamera.setPositionZ(myCamera.getPositionZ()+myKart.accelVector.getZ()*offset);
    		moveByLocalAxis(Utils.NORTH);
    		if(!b_accel)
    			myKart.accel-=.0002f;
    			
    		//serialize and send the object
    		float kartPosition[] = new float[3];
    		kartPosition[0] = myKart.getPosition().getX();
    		kartPosition[1] = myKart.getPosition().getY();
    		kartPosition[2] = myKart.getPosition().getZ();
    		
    		float kartAccelVector[] = new float[3];
    		kartAccelVector[0] = myKart.accelVector.getX();
    		kartAccelVector[1] = myKart.accelVector.getY();
    		kartAccelVector[2] = myKart.accelVector.getZ();
    		
    		KartSerializedInfo info = new KartSerializedInfo(kartPosition,myKart.getHeading(),kartAccelVector);
    		
    	}
    	
    	//check collision with other karts
    
    		
    }
    public void rotate(float angle){
    	
    
    	/*
    	 *  | x | * | cos@  sin@ | =  (x*cos@)+(z*-sin@)
    	 *  | z |   |-sin@  cos@ | =  (x*sin@)+(z*cos@)
    	 *
    	 */
       angle = (float)Math.toRadians(angle);
    	//get distance Vector
       float tmp[] = new float[2];
       float distanceVector[] = new float[2];
       float resultVector[] = new float[2];
       
       tmp[0] = myKart.target.getX()-myKart.getPosition().getX();
       tmp[1] = myKart.target.getZ()-myKart.getPosition().getZ();
       distanceVector = tmp;
       //System.out.println("distance vector is: "+ distanceVector[0]+" "+ distanceVector[1]);
       //rotate distance vector the given angle
       resultVector[0] = distanceVector[0]*(float)Math.cos(angle)+distanceVector[1]*-(float)Math.sin(angle);
       resultVector[1] = distanceVector[0]*(float)Math.sin(angle)+distanceVector[1]*(float)Math.cos(angle);
   
      // System.out.println("Result Vector: "+resultVector[0]+" , "+resultVector[1]);
       
      
 		myKart.newTarget = new Vertex3f(resultVector[0],myKart.getPosition().getY(),resultVector[1]);
 		
 		myKart.target = new Vertex3f(myKart.getPosition().getX()+myKart.newTarget.getX(),myKart.getPosition().getY(),myKart.getPosition().getZ()+myKart.newTarget.getZ());
 		//System.out.println("Kart look at: "+myKart.target.getX()+","+myKart.target.getY()+","+myKart.target.getZ());     
    	
    	//myKart.directionVector=new Vertex3f(myKart.target.getX()-myKart.getPosition().getX(),0f,myKart.target.getZ()-myKart.getPosition().getZ());
        myKart.setHeading(myKart.getHeading()-(float)Math.toDegrees(angle));
        
        float transposeViewVector[] = new float[2];
         float directionVector[] = new float[2];
        myCamera.setTargetX(myKart.getPosition().getX());
        myCamera.setTargetZ(myKart.getPosition().getZ());
        
        transposeViewVector[0]= myCamera.getPositionX()-myCamera.target.getX();
        transposeViewVector[1]= myCamera.getPositionZ()-myCamera.target.getZ();
        
        directionVector = Utils.rotateVector2f(transposeViewVector,angle);
        //directionVector = Utils.normalize2f(directionVector);
        
        float point[] = new float[2];
        point[0]=myCamera.target.getX()+directionVector[0];
        point[1]=myCamera.target.getZ()+directionVector[1];
        //obtener el vector que va del position al punto nuevo
        directionVector[0] = point[0]-myCamera.getPositionX();
        directionVector[1] = point[1]-myCamera.getPositionZ();
        //mover la posicion de la camara con este vector
        myCamera.setPositionX(myCamera.getPositionX()+directionVector[0]);	
        myCamera.setPositionZ(myCamera.getPositionZ()+directionVector[1]);	
       
    }
    
    
    public void moveByLocalAxis(int dir){
 		
 		float directionVector[] = new float[2];
 		//float offset = MAX_SPEED*myKart.accel;
 		switch(dir){
 			
 			case Utils.NORTH:
 				directionVector[0]=myKart.target.getX()-myKart.getPosition().getX();
 				directionVector[1] = myKart.target.getZ()-myKart.getPosition().getZ();
 				//normalize
 				
 					
 				//System.out.println("directionVector2D "+directionVector[0]+","+directionVector[1]);
 				directionVector = Utils.normalize2f(directionVector);
 				//multiplicar el unitario por la nueva magnitud
 				//directionVector[0] = directionVector[0]*offset;
 				//directionVector[1] = directionVector[1]*offset;
 				/*
 				//sumar el nuevo vector de direccion a la posicion inicial y al target
 				myKart.getPosition().setX(myKart.getPosition().getX()+directionVector[0]);
 				myKart.getPosition().setZ(myKart.getPosition().getZ()+directionVector[1]);
 				
 				myKart.target.setX(myKart.target.getX()+directionVector[0]);
 				myKart.target.setZ(myKart.target.getZ()+directionVector[1]);
 			*/
 				break;
 				
 				
 			case Utils.SOUTH:
 				
 				directionVector[0]=myKart.target.getX()-myKart.getPosition().getX();
 				directionVector[1] = myKart.target.getZ()-myKart.getPosition().getZ();
 				//normalize
 				directionVector = Utils.normalize2f(directionVector);
 				//multiplicar el unitario por la nueva magnitud
 			//	directionVector[0] = directionVector[0]*offset;
 			//	directionVector[1] = directionVector[1]*offset;
 				//sumar el nuevo vector de direccion a la posicion inicial y al target
 				myKart.getPosition().setX(myKart.getPosition().getX()+-1*directionVector[0]);
 				myKart.getPosition().setZ(myKart.getPosition().getZ()+-1*directionVector[1]);
 				
 				myKart.target.setX(myKart.getPosition().getX()+directionVector[0]);
 				myKart.target.setZ(myKart.getPosition().getZ()+directionVector[1]);
 				
 				
 				break;	
 			
 			
 		}
 		
 		myKart.accelVector=new Vertex3f(directionVector[0],0f,directionVector[1]);
 		myCamera.setTargetX(myKart.getPosition().getX());
 		myCamera.setTargetZ(myKart.getPosition().getZ());
 	/*	myCamera.setPositionX(myCamera.getPositionX()+directionVector[0]);
 		myCamera.setPositionZ(myCamera.getPositionZ()+directionVector[1]);
 		*/
 		//System.out.println("kart position is:"+ myKart.getPosition().getX()+","+myKart.getPosition().getY()+","+myKart.getPosition().getZ());
 		
 	}
    
}
